<template>
    <el-card class="form-container" shadow="never">
        <el-form :model="brand" :rules="rules" ref="brandFrom" label-width="150px">
            <el-form-item label="品牌名称" :required="true">
                <el-input v-model="brand.name" placeholder="品牌名称"></el-input>
            </el-form-item>

            <el-form-item label="品牌首字母">
                <el-input v-model="brand.firstLetter" placeholder="品牌首字母"></el-input>
            </el-form-item>

            <el-form-item label="品牌LOGO" :required="true">
                <SingleUpload v-model="brand.logo"></SingleUpload>
            </el-form-item>

            <el-form-item label="品牌专区大图">
                <SingleUpload v-model="brand.bigPic"></SingleUpload>
            </el-form-item>

            <el-form-item label="品牌故事">
                <el-input v-model="brand.brandStory" placeholder="品牌故事"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="brand.sort" placeholder="排序"></el-input>
            </el-form-item>

            <el-form-item label="是否显示">
                <el-radio-group v-model="brand.showStatus">
                    <el-radio :label="1">是</el-radio>
                    <el-radio :label="0">否</el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="品牌制造商">
                <el-radio-group v-model="brand.factoryStatus">
                    <el-radio :label="1">是</el-radio>
                    <el-radio :label="0">否</el-radio>
                </el-radio-group>    
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="saveBrand('brandFrom')">提交</el-button>
                <el-button v-if="!isEdit" @click="resetForm('brandFrom')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script>
import SingleUpload from '@/components/upload/SingleUpload'

import {getBrandById, addPmsBrand, updatePmsBrand} from '@/api/pms/brand'

const defaultBrand={
    bigPic: '',
    brandStory: '',
    factoryStatus: 0,
    firstLetter: '',
    logo: '',
    name: '',
    showStatus: 0,
    sort: 0
};
export default {
    name: 'BrandDetail',
    components: {
        SingleUpload
    },
    props: {
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
        return {
            brand:Object.assign({}, defaultBrand),
            rules:{

            }
        }
    },
    created() {
        if (this.isEdit) {
            let id = this.$route.query.id;
            getBrandById(id).then(response => {
                this.brand = response.data.data;
            });
        }
    },
    methods: {
        saveBrand(formName) {
            this.$refs[formName].validate((valid) =>{
                if (valid) {
                    this.$confirm('是否提交数据', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(()=>{
                        // 修改
                        if (this.isEdit) {
                            updatePmsBrand(this.$route.query.id, this.brand).then(response => {
                                this.$refs[formName].resetFields();
                                this.$message({
                                    message: '修改成功',
                                    type: 'success',
                                    duration:1000
                                });
                                this.$router.back();
                            });
                        } else {// 添加
                            addPmsBrand(this.brand).then(response => {
                                this.$refs[formName].resetFields();
                                this.brand = Object.assign({},defaultBrand);
                                this.$message({
                                    message: '提交成功',
                                    type: 'success',
                                    duration:1000
                                });
                            })
                        }
                    })
                }
            });
        },
        resetForm(brandFrom) {
            this.$refs[brandFrom].resetFields();
            this.brand = Object.assign({},defaultBrand);
        }
    }
}
</script>
<style>

</style>